#!/usr/bin/env python3
import sqlite3
import os
from datetime import datetime

# 连接到容器内的数据库
db_path = './openmemory.db'
conn = sqlite3.connect(db_path)
cursor = conn.cursor()

# 查看apps详细信息
cursor.execute('''
    SELECT id, name, created_at, updated_at 
    FROM apps 
    ORDER BY created_at
''')
apps = cursor.fetchall()
print("应用详情:")
for app in apps:
    print(f"  - ID: {app[0]}")
    print(f"    名称: {app[1]}")
    print(f"    创建时间: {datetime.fromisoformat(app[2])}")
    print(f"    更新时间: {datetime.fromisoformat(app[3])}")
    print()

# 查看每个应用的记忆数量
cursor.execute('''
    SELECT a.id, a.name, COUNT(m.id) as memory_count
    FROM apps a
    LEFT JOIN memories m ON a.id = m.app_id
    GROUP BY a.id, a.name
    ORDER BY a.created_at
''')
app_memories = cursor.fetchall()
print("应用记忆统计:")
for app_mem in app_memories:
    print(f"  - {app_mem[1]} (ID: {app_mem[0]}): {app_mem[2]} 条记忆")

# 查看最近的记忆
cursor.execute('''
    SELECT m.id, m.content, m.created_at, a.name as app_name
    FROM memories m
    JOIN apps a ON m.app_id = a.id
    ORDER BY m.created_at DESC
    LIMIT 5
''')
recent_memories = cursor.fetchall()
print("\n最近的记忆:")
for mem in recent_memories:
    print(f"  - 应用: {mem[3]}")
    print(f"    内容: {mem[1][:100]}...")
    print(f"    时间: {datetime.fromisoformat(mem[2])}")
    print()

conn.close()